package com.gjzhao.fmmall.service;

import cn.hutool.json.JSONUtil;
import com.gjzhao.fmmall.entity.Orders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

/**
 * @Description: 订单消息监听类
 * @Author: gjzhao
 * @Date Created in 2022-02-19 5:12 下午
 */
@Service
@RabbitListener(queues = {"orderQueue","orderCheckQueue"}) // 监听队列 [可以配置多个队列: queues = {"orderQueue","msgQueue"}]
public class OrderService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @RabbitHandler //处理逻辑
    public void handleMessage(String orderStr) {

        //一旦监听到死信队列中的消息，表示死信队列到期，需要将消息转发到 orderCheckQueue 中进行消费
        logger.info("----监听到orderQueue或者orderCheckQueue队列中的消息：{}，开始保存订单-----", orderStr);

        //将json字符串转化为对象
        Orders order = JSONUtil.toBean(orderStr, Orders.class);

        logger.info("----消费消息:{}----", order);

        logger.info("-----保存订单完成----");

    }
}
